package com.freewater.springboot.demo.resource.resourceloader;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.io.Resource;

import java.io.IOException;
import java.nio.file.Files;

/**
 * <code>Resource template = ctx.getResource("some/resource/path/myTemplate.txt");</code>
 * Against a ClassPathXmlApplicationContext, that code returns a ClassPathResource. If the
 * same method were run against a FileSystemXmlApplicationContext instance,
 * it would return a FileSystemResource. For a WebApplicationContext, it would return a ServletContextResource.
 * It would similarly return appropriate objects for each context.
 * As a result, you can load resources in a fashion appropriate to the particular application context.
 */
// ApplicationContext实现了ResourceLoader接口
public class Main {
    public static void main(String[] args) throws IOException {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(AppConfig.class);
        final Resource resource = applicationContext.getResource("classpath:com/freewater/springboot/demo/resource/resourceloader/some.txt");
        //也可以不用classpath前缀，AnnotationConfigApplicationContext默认的就是ClassPathResource
        //final Resource resource = applicationContext.getResource("com/freewater/springboot/demo/resource/some.txt");
        System.out.println(Files.readAllLines(resource.getFile().toPath()));
    }
}
